﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class UserWork_MailListC : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }
  
    public void Bind()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select * from Draft where Username='"+Session["UserName"].ToString()+"'";
        SqlDataAdapter da = new SqlDataAdapter(sql,con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        int sid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string sql = "select Appurtenance from Draft where DraftId=" + sid + "";
        SqlCommand com = new SqlCommand(sql, con);
        SqlDataReader dr = com.ExecuteReader();
        dr.Read();
        string ss = dr["Appurtenance"].ToString();
        string[] a = ss.Split(';');
        string url = "";
        for (int i = 0; i < a.Length; i++)
        {
            string b = a[i];
            url = Server.MapPath("~") + "\\upfiles\\" + b;

            if (File.Exists(url))
            {
                File.Delete(url);
            }
        }

        SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con1.Open();
        string sql1 = "delete from Draft  where DraftId =" + sid + "";
        SqlCommand com1 = new SqlCommand(sql1, con1);
        com1.ExecuteNonQuery();
        Response.Write("<script>alert('删除成功！')</script>");
        con1.Close();
        Bind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int sid = Convert.ToInt32(GridView1.DataKeys[e.NewEditIndex].Value.ToString());
        Response.Redirect("../MailRead3.aspx?sid="+sid+"");
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
            }
        }

    }
 
    protected void bnQuery_ServerClick(object sender, EventArgs e)
    {
        DateTime FD = Convert.ToDateTime(this.txFDate.Value);
        DateTime TD = Convert.ToDateTime(this.txTDate.Value);
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();

        string sql = "select * from Draft where addDate between '" + FD + "' and '" + TD + "' and sendname='" + Session["UserName"].ToString() + "'";
        SqlDataAdapter da = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
}
